/**
 * Created by HY on 2015/2/6 0006.
 */

var roleList = [], locationList = [], incidentList = [], tagList = [];
var initData;


$(function () {
	feedBind();
	commentBind();
	dataInit();


	//分享框点击显示事件
	$(".notice").on("mousedown", function () {
		if ($(this).attr("class") == "notice") {
			$(this).removeClass("notice").addClass("note-content").addClass("form-control").html("");
			$(".note-custom").css("display", "block");
			//先对标签数据进行缓存
			$(".note-tag").each(function (i, item) {
				noteTagId = getId(item);
				tabsRequest(item);
			})
		}
	});

	//回复框点击事件
	$(".comment-notice").on("mousedown", function () {
		$(this).removeClass("comment-notice").addClass("comment-content").html("");
		var id = $(this).parents("div .note-media").attr("id");

		var comment_form = '<form class="content-form" action="/parnote/comment" method="post">'
			+ '<div id="comment-content" class="comment-content" contenteditable="true"></div>'
			+ '<div class="comment-tool">'
			+ '<button class="btn btn-primary" type="button" name="submit" >回复</button>'
			+ '<button class="btn btn-default" style="margin-left: 10px;" name="cancel" type="button">取消</button>'
			+ '</div>'
			+ '</form>';
		$(this).parent(".note-comment-box").html(comment_form);
	});

	$("#note-content,.note-tag").on("blur", function (e) {

		var note = $("#note-content").html();
		note = filterHtml(note);
		note = filterSpace(note);

		var role = $("#note-role").val();
		var location = $("#note-location").val();
		var incident = $("#note-incident").val();

		obj = e.relatedTarget;
		var targetId = $(obj).attr("id");
		if (targetId == "note-content" || targetId == "note-role" || targetId == "note-location" || targetId == "note-incident" || targetId == "share") {
		} else {
			if (note.length == 0 && role.length == 0 && location.length == 0 && incident.length == 0) {
				var notice = "<div>分享,就是现在</div>";
				$(".note-content").removeClass().addClass("notice").html("").append(notice);
				$(".note-custom").css("display", "none");
			}
		}
	});
});

jQuery(function ($) {
	$(document).ready(function () {
		/*$(".custom-tabs").stickUp({
		 marginTop: '500px'
		 });*/

		$('input').iCheck({
			checkboxClass: 'icheckbox_square',
			radioClass: 'iradio_square-blue',
			increaseArea: '20%' // optional
		});

		$('#share').on('click', function () {
			var $btn = $(this).button('loading');
			var content = getHtml("note-content");
			content = filterHtml(content);
			var params = {content: content};
			ajaxSubmitJson("note-form", params, function (data) {
				if (data.success) {
					window.location.reload();
				}
				$btn.button('reset');
			})

		});

		$(".note-tag").typeahead({
			source: function (query, process) {
				var e = window.event;
				obj = e.currentTarget;
				noteTagId = getId(obj);
				map = {};
				tags = [];
				//从缓存里拿取
				tabsRequest(obj);
				$.each(tagList, function (i, tag) {
					map[tag.tagName] = tag;
					tags.push(tag.tagName);
				});
				process(tags);
			},
			matcher: function (item) {
				if (item.toLowerCase().indexOf(this.query.trim().toLowerCase()) != -1) {
					return true;
				}
			},
			sorter: function (items) {
				return items.sort();
			},
			updater: function (item) {
				$("#" + noteTagId + "-ids").val(map[item].tagIds);
				return item;
			}
		});
	});


});

function feedBind() {
	$(".feed").on("click", function () {
		feed(this);
	})
}

function feed(obj) {
	var feed_type = $(obj).attr("class").split(" ")[1];
	if (feed_type != "like" && feed_type != "unlike") {
		return;
	}
	var todo_id = $(obj).parent("p").attr("id");
	$.ajax({
		url: "feed",
		data: {
			todoId: todo_id,
			type: feed_type
		},
		type: "POST",
		dataType: "json",
		success: function (data) {
			if (!data.success()) {
				alert("操作失败");
			}

		}
	})
}

function commentBind() {
	//回复
	$(".note-comment-box").on("click", "[name='submit']", function () {
		comment(this);
	});

	//取消
	$(".note-comment-box").on("click", "[name='cancel']", function () {

	});
}

function comment(obj) {
	var id = $(obj).parents("div .note-media").attr("id");
	var form = $(obj).parents("form");
	var content = filterHtml($(form).find(".comment-content").html());
	var param_json = {noteId: id, authenticity_token: initData.authenticity_token, content: content};
//	alert(param_json);
	ajaxObjectSubmitJson(form, param_json, commentResponse);
}

function commentResponse(data) {
	alert("回复成功");
	window.location.reload();
}

function dataInit() {
	initData = JSON.parse($("#init-data").val());
}

/**
 * 这个方法在第一次点击分享框时请求3个类型的所有数据
 * 然后将这些数据缓存在JS里
 * 进行标签输入时返回缓存
 */
function tabsRequest(tag) {
	if ((noteTagId == "note-role" && roleList.length == 0) || (noteTagId == "note-location" && locationList.length == 0) || (noteTagId == "note-incident" && incidentList.length == 0)) {
		$.ajax({
			url: $(tag).attr("data-href"),
			async: false,
			success: tabsReponse
		});
	}
	else {
		if (noteTagId == "note-role") {
			tagList = roleList;
		} else if (noteTagId == "note-location") {
			tagList = locationList;
		} else if (noteTagId == "note-incident") {
			tagList = incidentList;
		}
	}
}

function tabsReponse(data) {
	//由于后台返回的是string类型,所以需要json转换
	var jsonData = JSON.parse(data);
	if (noteTagId == "note-role") {
		roleList = jsonData;
	} else if (noteTagId == "note-location") {
		locationList = jsonData;
	} else if (noteTagId == "note-incident") {
		incidentList = jsonData;
	}
}